package org.example.trainTicketSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.trainTicketSystem.DTO.GetScheduleDTO;
import org.example.trainTicketSystem.entity.TrainSchedule;
import org.example.trainTicketSystem.mapper.TrainScheduleMapper;
import org.example.trainTicketSystem.service.TrainScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainScheduleServiceImpl extends ServiceImpl<TrainScheduleMapper, TrainSchedule> implements TrainScheduleService {

    @Autowired
    private TrainScheduleMapper trainScheduleMapper;

    @Override
    public Page<TrainSchedule> selectTrainSchedule(GetScheduleDTO getScheduleDTO, int page, int size) {
        QueryWrapper<TrainSchedule> queryWrapper = new QueryWrapper<>();
        Page<TrainSchedule> trainSchedulePage = new Page<>(page, size);

        queryWrapper
                .like(StringUtils.isNotBlank(getScheduleDTO.getStartStation()), "start_station", getScheduleDTO.getStartStation())
                .like(StringUtils.isNotBlank(getScheduleDTO.getEndStation()), "end_station", getScheduleDTO.getEndStation())
                .ge(getScheduleDTO.getDepartureTime() != null, "departure_time", getScheduleDTO.getDepartureTime())
                .le(getScheduleDTO.getArrivalTime() != null, "arrival_time", getScheduleDTO.getArrivalTime());

        return trainScheduleMapper.selectPage(trainSchedulePage, queryWrapper);
    }

    @Override
    public Page<TrainSchedule> selectTrainScheduleByTrainNumber(String trainNumber,int page,int size) {
        QueryWrapper<TrainSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("train_number", trainNumber);
        Page<TrainSchedule> trainSchedulePage = new Page<>(page, size);
        return trainScheduleMapper.selectPage(trainSchedulePage,queryWrapper);
    }

    @Override
    public Page<TrainSchedule> selectAllTrainSchedule(int page, int size) {
        QueryWrapper<TrainSchedule> queryWrapper = new QueryWrapper<>();
        Page<TrainSchedule> trainSchedulePage = new Page<>(page, size);
        return trainScheduleMapper.selectPage(trainSchedulePage, queryWrapper);
    }
}
